package com.ddogring.web.controller.business;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ddogring.business.domain.Comment;
import com.ddogring.business.service.ICommentService;
import com.ddogring.common.core.controller.BaseController;
import com.ddogring.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @BelongsProject: eshop
 * @BelongsPackage: com.ddogring.web.controller.business
 * @Author: DdogRing
 * @CreateTime: 2023-02-02  17:02
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/comment")
public class CommentController extends BaseController {

    @Autowired
    private ICommentService commentService;

    /**
     * @description: 新增评论
     * @author: DdogRing
     * @date: 2023/2/2 17:11
     * @param: [comment]
     * @return: com.ddogring.common.core.domain.AjaxResult
     **/
    @PostMapping("/save")
    public AjaxResult saveComment(@Valid @RequestBody Comment comment) {

        // 点赞数默认为0
        comment.setSupport(0);
        if (null == comment.getStar()) {
            // 系统默认好评
            comment.setStatus(1);
        } else if (5 <= comment.getStar()) {
            comment.setStatus(1);
        } else if (3 <= comment.getStar()) {
            // 中评
            comment.setStatus(2);
        } else {
            // 差评
            comment.setStatus(3);
        }
        commentService.save(comment);
        return AjaxResult.success();
    }

    /**
     * @description: 单商品评价列表
     * @author: DdogRing
     * @date: 2023/2/9 17:39
     * @param: []
     * @return: com.ddogring.common.core.domain.AjaxResult
     **/
    @PostMapping("/commentList")
    public AjaxResult commentList(@RequestBody Comment comment) {

        if (null == comment || null == comment.getCommodityId()) {
            return AjaxResult.error("该商品不存在");
        }
        return AjaxResult.success(commentService.getCommentByCommodityId(comment.getCommodityId()));
    }

    /**
     * @description: 删除评论
     * @author: DdogRing
     * @date: 2023/2/9 19:50
     * @param: [comment]
     * @return: com.ddogring.common.core.domain.AjaxResult
     **/
    @PostMapping("/commentRemove")
    public AjaxResult commentRemove(@RequestBody Comment comment) {

        if (null == comment || null == comment.getId()) {
            return AjaxResult.error("该评论不存在");
        }

        UpdateWrapper<Comment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("del_flag", 1);
        updateWrapper.eq("id", comment.getId());

        return toAjax(commentService.update(updateWrapper));
    }
}
